import handlers from "../LogParser/handlers";

export default function logParser(msgData) {
  try {
    if (msgData.type === "command") {
      const command = msgData.content;
      const [action, subAction, rawPara] = command.split(" ");
      const para = rawPara.split(",");
      for (const fn of handlers["command"][action][subAction]) {
        const r = fn(msgData, ...para);
        if (r !== null) return r;
      }
    } else if (msgData.type === "room") {
      const r = handlers.room(msgData);
      if (r !== null) return r;
    } else {
      throw "Error: 日志解析错误，未知的消息类型";
    }
  } catch (error) {
    if (error.name === "TypeError") {
      throw "Error: 解析日志发生错误";
    } else {
      throw error;
    }
  }
  throw "Error: 没有匹配的处理程序";
}